package gen.tools.todolist;

import gen.tools.Widgets;
import gen.tools.structures.I1S1;
import gen.tools.structures.I2S5;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class GenToDoList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GenToDoList() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		
		/*
		 * 1. 取得 此ID所擁有的角色
		 * 2. 至待辦事項(toDoList)中找尋待回答的事項
		 * 
		 * */ 
		
		final String empId = request.getParameter("empId");
		int localSystemId = Integer.parseInt( request.getParameter("localSystemId") );	//本機系統的系統ID
		
		String strSQL = "SELECT DISTINCT subsysId,roleId FROM mpemprolesubsys WHERE empId=? AND sysSN=?";
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		final ArrayList<I2S5> toDoList = new ArrayList<I2S5>();
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, empId);
			ps.setInt(2, localSystemId);
			rs = ps.executeQuery();
			
			final ArrayList<I1S1> roleINsubsyses = new ArrayList<I1S1>();
			
			while ( rs.next() ) {
				I1S1 roleINsubsys = new I1S1();
				roleINsubsys.setS1( rs.getString("subsysId") );
				roleINsubsys.setI1( rs.getInt("roleId") );
				roleINsubsyses.add(roleINsubsys);
			}
			
			if (roleINsubsyses.size() > 0) {	//存在待辦事項
				
				strSQL = "SELECT t1.toDoListId,t1.dataType,t1.masterId,t1.cDT,t2.empName,t3.subsysDesc FROM todolist t1,emps t2,subsyses t3 WHERE t3.subsysId=t1.subsysId AND t1.creatorId=t2.empId AND t1.subsysId=? AND t1.roleId=? ORDER BY t1.cDT DESC";
				ps = conn.prepareStatement(strSQL);
				
				for(int i=0;i < roleINsubsyses.size(); i++ ) {
					ps.setString(1, roleINsubsyses.get(i).getS1() );
					ps.setInt(2, roleINsubsyses.get(i).getI1());
					rs = ps.executeQuery();
					while(rs.next()) {
						final I2S5 toDoItem = new I2S5();
						toDoItem.setS5( rs.getString("masterId") );								//單號
						toDoItem.setI2( rs.getInt("dataType") );								//主檔類型
						toDoItem.setI1( rs.getInt("toDoListId") );
						toDoItem.setS1( wid.getToDoListDataTypeDesc( rs.getInt("dataType")) );	//主檔類型描述文字
						toDoItem.setS2( wid.changeTimeFormat( rs.getTimestamp("cDT")) );		//創建日期
						toDoItem.setS3( rs.getString("empName") );								//創建人員
						toDoItem.setS4( rs.getString("subsysDesc") );
						toDoList.add(toDoItem);
					}
				}
			}
		} catch(Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		response.getWriter().write( new Gson().toJson(toDoList) );
	
	}

}
